library(foreign)
library(ggplot2)
library(grid)
library(gridExtra)
library(scales)

rm(list=ls())

setwd("Yourpath")

source("fine_grid.R") # ggplot layers
source("summarySE.R") # summary stats 

###############################################################
#                                                             #
#      PLOT TURNOUT GAP AS FCT OF SIGNATURES  (Figure 3)      #
#                                                             #
###############################################################

Y_0 = read.csv("Y_synthetic_nonannual.txt", sep = "\t")[,2]
Y_1 = read.csv("Y_treated_nonannual.txt", sep = "\t")[,2]  
X=read.csv("Y_treated_nonannual.txt", sep = "\t")[,1] 


data1 <- read.dta("data2a.dta",convert.factors = F)
refnum_to_anr <- read.dta("data2b.dta",convert.factors = F)


df <- data.frame(anrbfs=data1$anr,
                 datum=data1$datum,
                 jalager=data1$jalager, 
                 neinlager=data1$neinlager,
                 neutral=data1$neutral, 
                 unbestimmt=data1$unbestimmt,
                 bet=data1$bet,
                 unter_g=data1$unter_g,
                 rechtsform=data1$rechtsform)

df <- df[df$anrbfs>56& df$anrbfs<229,]

synth <- data.frame(Y_0=Y_0,
                    Y_1=Y_1,
                    gap=Y_1-Y_0,
                    refnum=X)

synth_anr <- merge(synth, refnum_to_anr, by=c("refnum"))


data_all <- merge(df, synth_anr, by=c("anrbfs"))


data_all$unsicher <- data_all$unbestimmt+data_all$neutral-(abs(data_all$jalager-data_all$neinlager))
data_all$winning_margin <- (abs(data_all$jalager-data_all$neinlager))


data_treatment_period <- data_all[is.element(data_all$refnum,c(33:59,66:69)),]

data_treatment_period$x <- (data_treatment_period$unter_g)/1000

lm(data_treatment_period$gap~data_treatment_period$x)

data_treatment_period_200=data_treatment_period[data_treatment_period$x<=200,]

# plot 1: create a ggplot object with a linear smoother and a CI

gg <- ggplot(data_treatment_period,aes(x=x,y=gap*100)) +  
  geom_point(size=4) + 
  geom_smooth(method="lm",colour="black",fill=NA)
gg


# plot 1: grab the data from the plot object
gg_data <- ggplot_build(gg)
str(gg_data)
head(gg_data$data[[2]])
gg2 <- gg_data$data[[2]]

# plot 2: create a ggplot object with a linear smoother and a CI

gg_plot2 <- ggplot(data_treatment_period_200,aes(x=x,y=gap*100)) +  
  geom_point(size=4) + 
  geom_smooth(method="lm",colour="black",fill=NA)
gg_plot2

# plot 2: grab the data from the plot object
gg_data2 <- ggplot_build(gg_plot2)
str(gg_data2)
head(gg_data2$data[[2]])
gg3 <- gg_data2$data[[2]]


# plot with 'CI-lines' but without confidence area
ggplot(data = data_treatment_period, aes(x = x, y = gap*100)) +
  geom_point(size=4) +
  theme_bw_finegrid(base_size = 32) +       
  geom_smooth(method = "lm", se = FALSE, colour="black", size = 1,linetype="dashed") +
  geom_smooth(data=data_treatment_period_200, se = FALSE, method= "lm", colour="black", size = 1) +
  geom_line(data = gg2, aes(x = x, y = ymin), size = 0.2,linetype="dashed") +
  geom_line(data = gg2, aes(x = x, y = ymax), size = 0.2,linetype="dashed") +
  geom_line(data = gg3, aes(x = x, y = ymin), size = 0.04) +
  geom_line(data = gg3, aes(x = x, y = ymax), size = 0.04) +
  scale_x_continuous(labels = comma,lim=c(30,410),breaks=c(seq(50,450,50))) +
  ylab("Effect on turnout (percentage points)\n") + xlab("Number of signatures (in thousands)")  +
  scale_y_continuous() 

ggsave(file="Figure3.pdf",width=12,height=10 )


